#include "includes.h"


int park_commu_get_listening_port()
{
	return 10086;
}

int park_commu_get_connect_port()
{
	return stPark.server_port;
}

int park_commu_get_connect_ip(char *ip)
{
	strncpy(ip, stPark.server_addr, 20);
	return 0;
}

void park_commu_init()
{
	//int ret;
	int port;
	
	if (net_init(&stPark.stNet) == -1)
	{
		ERR_SYS("net_init error");
	}
	stPark.listen_fd.s_type = NET_ST_SERVER;
	stPark.listen_fd.in_event = park_commu_deal;
	port = park_commu_get_listening_port();
	if (net_socket_init(&stPark.stNet, &stPark.listen_fd, NULL, port) == -1)
	{
		ERR_SYS("net_socket_init error");
	}
	return;
}

static int park_commu_send_packet(char *buff)
{
	char ip_addr[20];
	int port;
	int ret;

	printf("*****************send*******************\n");
	printf("%s", buff);
	printf("\n****************************************\n");
	ret = net_socket_send(&stPark.connect_fd, buff);
	if (ret == -1)
	{
		net_socket_destroy(&stPark.connect_fd);
		bzero(&stPark.connect_fd, sizeof(NET_SOCKET_T));
		stPark.connect_fd.s_type = NET_ST_CLIENT;
		stPark.connect_fd.in_event = park_commu_deal;
		park_commu_get_connect_ip(ip_addr);
		port = park_commu_get_connect_port();
		if (net_socket_init(&stPark.stNet, &stPark.connect_fd, ip_addr, port)!=0)
		{
			return -1;
		}

		ret = net_socket_send(&stPark.connect_fd, buff);
	}
	return ret;
}

int park_commu_send_requst_packet()
{
	int ret;
	char buff[400];
	bzero(buff, sizeof(buff));
	park_seq_req();
	snprintf(buff, sizeof(buff), "<?xml version=\"1.0\" encoding=\"utf-8\"?> <request> <command_id>%d</command_id> <sequence_id>%d</sequence_id> </request>", COMMU_TERMINAL_TO_SERVER_ALL_REQ, stPark.seq_req);
	ret = park_commu_send_packet(buff);
	return ret;
}

static int park_commu_send_ack_packet(int command_id, int status)
{
	int ret;
	char buff[400];
	bzero(buff, sizeof(buff));
	snprintf(buff, sizeof(buff), "<?xml version=\"1.0\" encoding=\"utf-8\"?><resp><command_id>%d</command_id><sequence_id>%d</sequence_id><body><status>%d</status><msg></msg></body></resp>", command_id, stPark.seq_ack, status);
	ret = park_commu_send_packet(buff);
	return ret;
}

/*
static int park_commu_send_trap_err_packet(int status)
{
	int ret;
	char buff[400];
	bzero(buff, sizeof(buff));
	snprintf(buff, sizeof(buff), "<?xml version=\"1.0\" encoding=\"utf-8\"?><resp><command_id>%d</command_id><sequence_id>%d</sequence_id><body><status>%d</status><msg></msg></body></resp>", COMMU_TERMINAL_TO_SERVER_ALL_ACK, stPark.seq_ack, status);
	ret = park_commu_send_packet(buff);
	return ret;
}
*/

int park_commu_decode(mxml_node_t *pstBodyNode)
{
	mxml_node_t *pstFloorNode, *pstAreaNode, *pstProbeNode;
	mxml_node_t *pstSensorNode, *pstLedNode, *pstDirectNode, *pstDirectAreaNode;
	mxml_node_t *pstChildFloorNode, *pstChildAreaNode, *pstChildProberNode, *pstChildSensorNode;
	mxml_node_t *pstChildLedNode, *pstChildDirectNode;
	PS_FLOOR pstFloor;
	PS_AREA pstArea;
	PS_PROBER pstProber;
	PS_SENSOR pstSensor;
	PS_LED pstLed;
	PS_DIRECT pstDirect;
	
	int sensor_io;
	int direct_type;
	int direct_area_id;
	const char *pcElement_name;
	struct list_head *pstList;
	PS_AREA pstAreaDirect;

	stPark.led_cnt = stPark.area_cnt = stPark.floor_cnt = stPark.sensor_cnt = stPark.prober_cnt = 0;
	
	pstFloorNode = NULL;
	for (pstFloorNode = mxmlFindElement(pstBodyNode, pstBodyNode, "floor", NULL, NULL, MXML_DESCEND);\
		 pstFloorNode != NULL;\
		 pstFloorNode = mxmlFindElement(pstFloorNode, pstBodyNode, "floor", NULL, NULL, MXML_NO_DESCEND))
	{
	
		pstFloor = Malloc(sizeof(S_FLOOR));
		bzero(pstFloor, sizeof(S_FLOOR));
		stPark.floor_cnt++;
		list_add_tail(&pstFloor->floor_list, &stPark.floor_list);
		INIT_LIST_HEAD(&pstFloor->area_list);

		for (pstChildFloorNode = mxmlGetFirstChild(pstFloorNode); pstChildFloorNode != NULL; pstChildFloorNode = mxmlGetNextSibling(pstChildFloorNode))
		{
			
			pcElement_name = mxmlGetElement(pstChildFloorNode);
			if (strncmp(pcElement_name, "id", strlen(pcElement_name)) == 0)
			{
				pstFloor->floor_id = atoi(mxmlGetText(pstChildFloorNode,0));
			}
			else if (strncmp(pcElement_name, "name", strlen(pcElement_name)) == 0)
			{
				strncpy(pstFloor->floor_name, mxmlGetText(pstChildFloorNode,0), NAME_LEN);
			}
			else if (strncmp(pcElement_name, "no", strlen(pcElement_name)) == 0)
			{
				pstFloor->floor_no = atoi(mxmlGetText(pstChildFloorNode,0));
			}			
			else if (strncmp(pcElement_name, "areas", strlen(pcElement_name)) == 0)
			{

				pstAreaNode = NULL;
				for (pstAreaNode = mxmlFindElement(pstChildFloorNode, pstChildFloorNode, "area", NULL, NULL, MXML_DESCEND);\
		 			pstAreaNode != NULL;\
		 			pstAreaNode = mxmlFindElement(pstAreaNode, pstChildFloorNode, "area", NULL, NULL, MXML_NO_DESCEND))
		 			{
		 			
		 				pstArea = Malloc(sizeof(S_AREA));
		 				bzero(pstArea, sizeof(S_AREA));
		 				
						stPark.area_cnt++;
						list_add_tail(&pstArea->area_sys_list, &stPark.area_list);
						list_add_tail(&pstArea->area_list, &pstFloor->area_list);
						INIT_LIST_HEAD(&pstArea->prober_list);
						INIT_LIST_HEAD(&pstArea->led_list);
						INIT_LIST_HEAD(&pstArea->area_direct_list);
						
						for (pstChildAreaNode = mxmlGetFirstChild(pstAreaNode); pstChildAreaNode != NULL; pstChildAreaNode = mxmlGetNextSibling(pstChildAreaNode))
						{
							pcElement_name = mxmlGetElement(pstChildAreaNode);
							if (strncmp(pcElement_name, "areaid", strlen(pcElement_name)) == 0)
							{
								pstArea->area_id = atoi(mxmlGetText(pstChildAreaNode,0));
							}
							else if (strncmp(pcElement_name, "areaname", strlen(pcElement_name)) == 0)
							{
								strncpy(pstArea->area_name, mxmlGetText(pstChildAreaNode,0), NAME_LEN);
							}
							else if (strncmp(pcElement_name, "equipments", strlen(pcElement_name)) == 0)
							{

								pstProbeNode = NULL;
								for (pstProbeNode = mxmlFindElement(pstChildAreaNode, pstChildAreaNode, "equipment", NULL, NULL, MXML_DESCEND);\
		 							 pstProbeNode != NULL;\
		 							 pstProbeNode = mxmlFindElement(pstProbeNode, pstChildAreaNode, "equipment", NULL, NULL, MXML_NO_DESCEND))
		 						{
		 							
		 							pstProber = Malloc(sizeof(S_PROBER));
		 							bzero(pstProber, sizeof(S_PROBER));
									pstProber->dev_type = PARK_DEV_TYPE_PROBER;

									stPark.prober_cnt++;
									list_add_tail(&pstProber->prober_list, &pstArea->prober_list);
		 							INIT_LIST_HEAD(&pstProber->sensor_list);
		 							
		 							for (pstChildProberNode = mxmlGetFirstChild(pstProbeNode); pstChildProberNode != NULL; pstChildProberNode = mxmlGetNextSibling(pstChildProberNode))
		 							{
		 								pcElement_name = mxmlGetElement(pstChildProberNode);
										if (strncmp(pcElement_name, "id", strlen(pcElement_name)) == 0)
										{
											pstProber->prober_id = atoi(mxmlGetText(pstChildProberNode,0));
											if (hash_insert_list(stPark.pstHash, (void *)&pstProber->prober_id, (void *)pstProber) == NULL)
											{
												ERR_SYS("hash_insert_list error!!!");
											}
										}
										else if (strncmp(pcElement_name, "type", strlen(pcElement_name)) == 0)
										{
											pstProber->prober_type = atoi(mxmlGetText(pstChildProberNode,0));
										}
										else if (strncmp(pcElement_name, "ip", strlen(pcElement_name)) == 0)
										{
											strncpy(pstProber->ip_addr, mxmlGetText(pstChildProberNode,0), 20);
										}
										else if (strncmp(pcElement_name, "areaid", strlen(pcElement_name)) == 0)
										{
											pstProber->area_id = atoi(mxmlGetText(pstChildProberNode,0));
										}
										else if (strncmp(pcElement_name, "sensors", strlen(pcElement_name)) == 0)
										{

											pstSensorNode = NULL;
											for (pstSensorNode = mxmlFindElement(pstChildProberNode, pstChildProberNode, "sensor", NULL, NULL, MXML_DESCEND);\
		 							 			 pstSensorNode != NULL;\
		 							 			 pstSensorNode = mxmlFindElement(pstSensorNode, pstChildProberNode, "sensor", NULL, NULL, MXML_NO_DESCEND))
		 							 		{

		 							 			pstSensor = Malloc(sizeof(S_SENSOR));
		 										bzero(pstSensor, sizeof(S_SENSOR));
												pstSensor->dev_type = PARK_DEV_TYPE_SENSOR;
												stPark.sensor_cnt++;
												pstProber->sensor_cnt++;
												list_add_tail(&pstSensor->sensor_list, &pstProber->sensor_list);

		 										for (pstChildSensorNode = mxmlGetFirstChild(pstSensorNode); pstChildSensorNode != NULL; pstChildSensorNode = mxmlGetNextSibling(pstChildSensorNode))
		 										{
		 											pcElement_name = mxmlGetElement(pstChildSensorNode);
		 											if (strncmp(pcElement_name, "limitheight", strlen(pcElement_name)) == 0)
		 											{
		 												pstSensor->high = atoi(mxmlGetText(pstChildSensorNode,0));
		 											}
		 											else if (strncmp(pcElement_name, "id", strlen(pcElement_name)) == 0)
		 											{
		 												pstSensor->sensor_id = atoi(mxmlGetText(pstChildSensorNode,0));
		 												if (hash_search_list(stPark.pstHash, (void *)&pstSensor->sensor_id) != NULL)
		 												{
		 													DBG(" %d is occuped\n", pstSensor->sensor_id);
		 												}
		 												if (hash_insert_list(stPark.pstHash, (void *)&pstSensor->sensor_id, (void *)pstSensor) == NULL)
														{
															ERR_SYS("hash_insert_list error!!! sensor");
														}
		 											}
		 											else if (strncmp(pcElement_name, "type", strlen(pcElement_name)) == 0)
		 											{
		 												pstSensor->sensor_type = atoi(mxmlGetText(pstChildSensorNode,0));
		 											}
		 											else if (strncmp(pcElement_name, "io_addr", strlen(pcElement_name)) == 0)
		 											{
		 												sensor_io = pstSensor->io_addr = atoi(mxmlGetText(pstChildSensorNode,0));
		 											}
		 											else if (strncmp(pcElement_name, "host_addr", strlen(pcElement_name)) == 0)
		 											{
		 												pstSensor->host_addr = atoi(mxmlGetText(pstChildSensorNode,0));
		 											}
		 											else
		 											{
		 												err_msg("sensor illegal child %s", pcElement_name);
		 											}
		 										}

		 							 		}
										}
										else
										{
											err_msg("prober illegal child %s", pcElement_name);
										}
		 							
		 							}
						
		 						}
							}
							else
							{
								err_msg("area illegal child %s", pcElement_name);
							}
							
						}

		 			}
			}
			else
			{
				err_msg("floor illegal child %s", pcElement_name);
			}
			
		}
	}

	pstLedNode = NULL;
	for (pstLedNode = mxmlFindElement(pstBodyNode, pstBodyNode, "led", NULL, NULL, MXML_DESCEND);\
		 pstLedNode != NULL;\
		 pstLedNode = mxmlFindElement(pstLedNode, pstBodyNode, "led", NULL, NULL, MXML_NO_DESCEND))
	{
		pstLed = Malloc(sizeof(S_LED));
		bzero(pstLed, sizeof(S_LED));

		stPark.led_cnt++;
		list_add_tail(&pstLed->led_list, &stPark.led_list);
		//INIT_LIST_HEAD(pstLed.)

		for (pstChildLedNode = mxmlGetFirstChild(pstLedNode); pstChildLedNode != NULL; pstChildLedNode = mxmlGetNextSibling(pstChildLedNode))
		{
			pcElement_name = mxmlGetElement(pstChildLedNode);
			if (strncmp(pcElement_name, "id", strlen(pcElement_name)) == 0)
			{
				pstLed->led_id = atoi(mxmlGetText(pstChildLedNode,0));
			}
			else if (strncmp(pcElement_name, "type", strlen(pcElement_name)) == 0)
			{
				pstLed->led_type = atoi(mxmlGetText(pstChildLedNode,0));
			}
			else if (strncmp(pcElement_name, "ip", strlen(pcElement_name)) == 0)
			{
				strncpy(pstLed->ip_addr, mxmlGetText(pstChildLedNode,0), 20);
			}
			else if (strncmp(pcElement_name, "directions", strlen(pcElement_name)) == 0)
			{

				pstDirectNode = NULL;
				for (pstDirectNode = mxmlFindElement(pstChildLedNode, pstChildLedNode, "direction", NULL, NULL, MXML_DESCEND);\
		 			 pstDirectNode != NULL;\
		 			 pstDirectNode = mxmlFindElement(pstDirectNode, pstChildLedNode, "direction", NULL, NULL, MXML_NO_DESCEND))
		 		{
		 			pstDirect = Malloc(sizeof(S_DIRECT));
					bzero(pstDirect, sizeof(S_DIRECT));

					INIT_LIST_HEAD(&pstDirect->area_list);
					direct_type = -1;
					for (pstChildDirectNode = mxmlGetFirstChild(pstDirectNode); pstChildDirectNode != NULL; pstChildDirectNode = mxmlGetNextSibling(pstChildDirectNode))
					{
						pcElement_name = mxmlGetElement(pstChildDirectNode);
						if (strncmp(pcElement_name, "type", strlen(pcElement_name)) == 0)
						{
							direct_type = pstDirect->direction_type = atoi(mxmlGetText(pstChildDirectNode,0));
							if (direct_type<0 || direct_type>=LED_DIRECT_MAX)
							{	
								break;
							}
							pstLed->pstDirect_a[direct_type] = pstDirect;
						}
						else if (strncmp(pcElement_name, "directionname", strlen(pcElement_name)) == 0)
						{
							strncpy(pstDirect->direction_name, mxmlGetText(pstChildDirectNode,0), NAME_LEN);
						}
						else if (strncmp(pcElement_name, "areas", strlen(pcElement_name)) == 0)
						{
							pstDirectAreaNode = NULL;
							for (pstDirectAreaNode = mxmlFindElement(pstChildDirectNode, pstChildDirectNode, "areaid", NULL, NULL, MXML_DESCEND);\
		 			 		pstDirectAreaNode != NULL;\
		 			 		pstDirectAreaNode = mxmlFindElement(pstDirectAreaNode, pstChildDirectNode, "areaid", NULL, NULL, MXML_NO_DESCEND))
		 			 		{
		 			 			direct_area_id = atoi(mxmlGetElement(pstDirectAreaNode));
		 			 			list_for_each(pstList, &stPark.area_list)
		 			 			{
		 			 				pstAreaDirect = (PS_AREA)list_entry(pstList, S_AREA, area_sys_list);
		 			 						 			 				
		 			 				if (pstAreaDirect->area_id == direct_area_id)
		 			 				{
		 			 					//pstDirect->ppstArea[pstDirect->area_num++] = pstAreaDirect;
		 			 					list_add_tail(&pstAreaDirect->area_direct_list, &pstDirect->area_list);
		 			 					list_add_tail(&pstLed->led_area_list, &pstAreaDirect->led_list);		 			 					
		 			 					break;
		 			 				}		 			 						 			 				
		 			 			}
		 			 		}
						}
						else
						{
							err_msg("direction illegal child %s", pcElement_name);
						}
					}
					if (direct_type<0 || direct_type>=LED_DIRECT_MAX)
					{
						err_msg("direction illegal type %d!", direct_type);
						free(pstDirect);
					}
		 		}
			}
			else
			{
				err_msg("led illegal child %s", pcElement_name);
			}
		}
		
	}

	DBG("floor=(%d) area=(%d) prober=(%d) sensor=(%d) led=(%d) \n", stPark.floor_cnt, stPark.area_cnt, stPark.prober_cnt, stPark.sensor_cnt, stPark.led_cnt);
	return 0;
}

int park_commu_free()
{
	struct list_head *pstFloorListPos, *pstFloorListNext;
	struct list_head *pstAreaListPos, *pstAreaListNext;
	struct list_head *pstProberListPos, *pstProberListNext;
	struct list_head *pstSensorListPos, *pstSensorListNext;
	struct list_head *pstLedListPos, *pstLedListNext;
	//struct list_head *pstDirectListPos, *pstDirectListNext;
	PS_FLOOR pstFloor;
	PS_AREA pstArea;
	PS_PROBER pstProber;
	PS_SENSOR pstSensor;
	PS_LED pstLed;
	//PS_DIRECT pstDirect;
	int i;



	list_for_each_safe(pstFloorListPos, pstFloorListNext, &stPark.floor_list)
	{
		pstFloor = list_entry(pstFloorListPos, S_FLOOR, floor_list);
		list_for_each_safe(pstAreaListPos, pstAreaListNext, &pstFloor->area_list)
		{
			pstArea = list_entry(pstAreaListPos, S_AREA, area_list);
			list_for_each_safe(pstProberListPos, pstProberListNext, &pstArea->prober_list)
			{
				pstProber = list_entry(pstProberListPos, S_PROBER, prober_list);
				list_for_each_safe(pstSensorListPos, pstSensorListNext, &pstProber->sensor_list)
				{
					pstSensor = list_entry(pstSensorListPos, S_SENSOR, sensor_list);
					free(pstSensor);
				}
				free(pstProber);
			}
			free(pstArea);
		}
		free(pstFloor);
	}
		

	
	


	list_for_each_safe(pstLedListPos, pstLedListNext, &stPark.led_list)
	{
		pstLed = list_entry(pstLedListPos, S_LED, led_list);
		for(i=0; i<LED_DIRECT_MAX; i++)
		{
			if (pstLed->pstDirect_a[i] == NULL)
				continue;
			free(pstLed->pstDirect_a[i]);
		}
		free(pstLed);
	}

	
	INIT_LIST_HEAD(&stPark.floor_list);
	INIT_LIST_HEAD(&stPark.led_list);
	INIT_LIST_HEAD(&stPark.area_list);

	return 0;
	
}


static int park_commu_send_trap()
{
	//int i;
	int ret;
	char *buff;
	char unit[70];
	int stat;
	struct list_head *pos, *subpos, *subsubpos;
	PS_AREA pstArea;
	PS_PROBER pstProber;
	PS_SENSOR pstSensor;
	PS_LED pstLed;
/*
	buff = (char *)Malloc(stPark.dev_offline_num*70 + 200);

	sprintf(buff, "<?xml version=\"1.0\" encoding=\"utf-8\" ?><resq><commandid>%d</commandid><sequence_id>%d</sequence_id><body><equipments>", COMMU_TERMINAL_TO_SERVER_TRAP_ACK, stPark.seq_ack);
	for (i=0; i<stPark.dev_offline_num; i++);
	{
		sprintf(unit, "<equipment><id>%x</id><status>6</status><msg></msg></equipment>", stPark.dev_offline_list[i]);
		strcat(buff, unit);
	}
	strcat(buff, "</equipments></body></resp>");

	ret = park_commu_send_packet(buff);
	free(buff);
*/
	buff = (char *)Malloc((stPark.sensor_cnt+stPark.prober_cnt+stPark.led_cnt)*70 + 200);
	sprintf(buff, "<?xml version=\"1.0\" encoding=\"utf-8\" ?><resp><command_id>%d</command_id><sequence_id>%d</sequence_id><body><equipments>", COMMU_TERMINAL_TO_SERVER_TRAP_ACK, stPark.seq_ack);
	list_for_each(pos, &stPark.area_list)
	{
		pstArea = list_entry(pos, S_AREA, area_sys_list);
		list_for_each(subpos, &pstArea->prober_list)
		{
			pstProber = list_entry(subpos, S_PROBER, prober_list);
			if (pstProber->stat == PROBER_STAT_OFFLINE)
				stat = 2;
			else
				stat = 1;
			sprintf(unit, "<equipment><id>%d</id><status>%d</status><msg></msg></equipment>", pstProber->prober_id, stat);
			strcat(buff, unit);
			list_for_each(subsubpos, &pstProber->sensor_list)
			{
				pstSensor = list_entry(subsubpos, S_SENSOR, sensor_list);
				sprintf(unit, "<equipment><id>%d</id><status>%d</status><msg></msg></equipment>", pstSensor->sensor_id, pstSensor->stat);
				strcat(buff, unit);
			}
		}
	}
	
	list_for_each(pos, &stPark.led_list)
	{
		pstLed = list_entry(pos, S_LED, led_list);
		sprintf(unit, "<equipment><id>%d</id><status>%d</status><msg></msg></equipment>", pstLed->led_id, pstLed->stat);
		strcat(buff, unit);
	}
	strcat(buff, "</equipments><status>0</status><msg></msg></body></resp>");
	ret = park_commu_send_packet(buff);
	free(buff);
	return ret;
}

void park_commu_deal(struct _NET_SOCKET_T *pstNetobject)
{
	mxml_node_t *pstTopNode, *pstCmdNode, *pstSeqNode, *pstBodyNode;
	int cmd, seq;
	//int i;
	
	printf("%s\n", pstNetobject->buf);
	pstTopNode = mxmlLoadString(NULL, pstNetobject->buf, MXML_NO_CALLBACK);
	if (pstTopNode == NULL)
	{
		err_msg("xml string load error!");
		return ;
	}

	pstCmdNode = mxmlFindElement(pstTopNode, pstTopNode, "command_id", NULL, NULL, MXML_DESCEND);
	pstSeqNode = mxmlFindElement(pstTopNode, pstTopNode, "sequence_id", NULL, NULL, MXML_DESCEND);
	
	if (pstCmdNode == NULL || pstSeqNode == NULL )
	{
		err_msg("xml head error!");
		mxmlDelete(pstTopNode);
		return ;
	}

	cmd = atoi(mxmlGetText(pstCmdNode,0));
	seq = atoi(mxmlGetText(pstSeqNode,0));

	switch(cmd)
	{
		case COMMU_SERVER_TO_TERMINAL_ALL_PUSH:
			pstBodyNode = mxmlFindElement(pstTopNode, pstTopNode, "body", NULL, NULL, MXML_DESCEND);
			if (pstBodyNode == NULL)
			{
				err_msg("can not find <body> node");
				mxmlDelete(pstTopNode);
				break ;
			}
			park_seq_push_set(seq);
			if (park_get_stat() != PARK_STAT_IDLE)
			{
				DBG("system is busy push the config\n");
				park_commu_send_ack_packet(COMMU_SERVER_TO_TERMINAL_ALL_ACK, 5);
				mxmlDelete(pstTopNode);
				break;
			}
			park_commu_send_ack_packet(COMMU_SERVER_TO_TERMINAL_ALL_ACK, 0);
			stPark.pstMxmlTopNode = pstTopNode;
			stPark.pstMxmlTmpNode = pstBodyNode;
			stPark.cur_config_id++;
			park_set_stat(PARK_STAT_PUSH);
			Sem_post(&stPark.park_sem);
			break;
		case COMMU_TERMINAL_TO_SERVER_ALL_ACK:
			pstBodyNode = mxmlFindElement(pstTopNode, pstTopNode, "body", NULL, NULL, MXML_DESCEND);
			if (pstBodyNode == NULL)
			{
				err_msg("can not find <body> node");
				mxmlDelete(pstTopNode);
				break ;
			}
			if (!park_seq_is_same(seq))
			{
				DBG("seq not right! %d != %d \n", park_seq_get(), seq);
				mxmlDelete(pstTopNode);
				break;
			}			
			stPark.pstMxmlTopNode = pstTopNode;
			stPark.pstMxmlTmpNode = pstBodyNode;
			stPark.cur_config_id++;
			park_set_stat(PARK_STAT_START);
			Sem_post(&stPark.park_sem);
			break;
		case COMMU_TERMINAL_TO_SERVER_TRAP_REQ:
			park_seq_push_set(seq);
			if (park_get_stat() != PARK_STAT_IDLE)
			{
				DBG("system is busy push the config\n");
				park_commu_send_ack_packet(COMMU_TERMINAL_TO_SERVER_TRAP_ACK, 5);
				mxmlDelete(pstTopNode);
				break;
			}
			park_commu_send_trap();
			mxmlDelete(pstTopNode);
			break;
		case COMMU_TERMINAL_TO_SERVER_SEND_ACK:
			mxmlDelete(pstTopNode);
			break;
		default:
			mxmlDelete(pstTopNode);
			err_msg("cmd is not supported!");
			break;
	}

	return;
}
